// src/main/java/com/xur/springboot/controller/ChatController.java
package com.xur.springboot.controller;

import com.xur.springboot.entity.ChatMessage;
import com.xur.springboot.service.ChatService;
import com.xur.springboot.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    @PostMapping("/send")
    public ResultVO sendMessage(@RequestBody ChatMessage message) {
        return chatService.sendMessage(message);
    }

    @GetMapping("/history")
    //public ResultVO getHistory(
    public List<ChatMessage> getHistory(
            @RequestParam Integer sender_id,
            @RequestParam Integer receiver_id) {
        return chatService.getHistory(sender_id, receiver_id);
    }

    @GetMapping("/selectChatBysender_id")
    //public ResultVO getHistory(
    public List<ChatMessage> selectChatBysender_id(
            @RequestParam Integer sender_id,
            @RequestParam Integer receiver_id) {
        return chatService.selectChatBysender_id(sender_id, receiver_id);
    }

    @GetMapping("/selectChatHistory")
    //public ResultVO getHistory(
    public List<ChatMessage> selectChatHistory(
            @RequestParam Integer sender_id,
            @RequestParam Integer receiver_id) {
        System.out.println(sender_id);
        return chatService.selectChatHistory(sender_id, receiver_id);
    }


    @GetMapping("/relatedUsers")
    public List<Integer> getRelatedUsers(@RequestParam Integer userId) {
        return chatService.getRelatedUserIds(userId);
    }

    @PutMapping("/read/{msg_id}")
    public ResultVO markAsRead(@PathVariable Integer msg_id) {
        return chatService.markAsRead(msg_id);
    }
}